package com.zjdiepu.www.biz.fk;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.AuditEditStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.fk.FkAuditConditionEditMapper;
import com.zjdiepu.www.dao.fk.FkAuditConditionEditSubMapper;
import com.zjdiepu.www.dao.other.OtherAuditInfoMapper;
import com.zjdiepu.www.dao.yw.YwBusinessExtrasMapper;
import com.zjdiepu.www.dao.yw.YwFeesSubMapper;
import com.zjdiepu.www.ibiz.fk.IFkAuditConditionEditService;
import com.zjdiepu.www.model.fk.dto.FkAuditConditionEdit;
import com.zjdiepu.www.model.fk.dto.FkAuditConditionEditSub;
import com.zjdiepu.www.model.fk.ext.FkAuditConditionEditExt;
import com.zjdiepu.www.model.other.dto.OtherAuditInfo;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessExtras;
import com.zjdiepu.www.model.yw.dto.YwFeesSub;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

/**
 * fk_风控条件修改Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2018年01月31日 15时48分<br/>
 * @since 1.0<br/>
 */
@Service
public class FkAuditConditionEditServiceImpl extends BaseService implements IFkAuditConditionEditService {
	public static final Logger logger = LoggerFactory.getLogger(FkAuditConditionEditServiceImpl.class);

	@Autowired
	private FkAuditConditionEditMapper fkAuditConditionEditMapper;
	@Autowired
	private OtherAuditInfoMapper otherAuditInfoMapper;
	@Autowired
	private YwBusinessExtrasMapper ywBusinessExtrasMapper;
	@Autowired
	private FkAuditConditionEditSubMapper fkAuditConditionEditSubMapper;
	@Autowired
	private YwFeesSubMapper ywFeesSubMapper;
	
	@Override
	public void saveUpdateAndDelete(FkAuditConditionEdit fkAuditConditionEdit, OperateType operType) {
		switch (operType) {
		case add:
			fkAuditConditionEdit.initInsertData(fkAuditConditionEdit.getModifier(), fkAuditConditionEdit.getModifierName());
			fkAuditConditionEditMapper.insert(fkAuditConditionEdit);
			break;
		case edit:
			fkAuditConditionEdit.initUpdateData(fkAuditConditionEdit.getModifier(), fkAuditConditionEdit.getModifierName());
			// 影响的记录数
			int updateCount = fkAuditConditionEditMapper.updateByPrimaryKeySelective(fkAuditConditionEdit);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			fkAuditConditionEdit.initDeleteData(fkAuditConditionEdit.getModifier(), fkAuditConditionEdit.getModifierName());
			// 影响的记录数
			int deleteCount = fkAuditConditionEditMapper.updateByPrimaryKeySelective(fkAuditConditionEdit);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<FkAuditConditionEdit> selectByExt(FkAuditConditionEditExt fkAuditConditionEditExt) {
		return fkAuditConditionEditMapper.selectByExt(fkAuditConditionEditExt);
	}
	
	@Override
	public int countByExt(FkAuditConditionEditExt fkAuditConditionEditExt){
		return fkAuditConditionEditMapper.countByExt(fkAuditConditionEditExt);
	}
	
	@Override
	public FkAuditConditionEdit selectById(String id) {
		return fkAuditConditionEditMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			fkAuditConditionEditMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(FkAuditConditionEdit fkAuditConditionEdit) {
		return fkAuditConditionEditMapper.countRefined(fkAuditConditionEdit);
	}

	@Override
	public FkAuditConditionEdit selectRefined(FkAuditConditionEdit fkAuditConditionEdit) {
		return fkAuditConditionEditMapper.selectRefined(fkAuditConditionEdit);
	}
	
	@Override
	public int deletedRefined(FkAuditConditionEdit fkAuditConditionEdit) {
		return fkAuditConditionEditMapper.deletedRefined(fkAuditConditionEdit);
	}

	@Override
	public boolean repeat(FkAuditConditionEdit fkAuditConditionEdit) {
		int count = countRefined(fkAuditConditionEdit);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(fkAuditConditionEdit.getId())) {
			return true;
		}
		FkAuditConditionEdit dbItem = selectRefined(fkAuditConditionEdit);
		if(null == dbItem) {
			return false;
		}
		return !fkAuditConditionEdit.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return fkAuditConditionEditMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<FkAuditConditionEdit> fkAuditConditionEdits) {
		return fkAuditConditionEditMapper.insertBatch(fkAuditConditionEdits);
	}

	@Override
	public int countMultiByExt(YwBusinessQueryRequest ywBusinessQueryRequest) {
		return fkAuditConditionEditMapper.countMultiByExt(ywBusinessQueryRequest);
	}

	@Override
	public List<YwBusiness> selectMultiByExt(
			YwBusinessQueryRequest ywBusinessQueryRequest) {
		return fkAuditConditionEditMapper.selectMultiByExt(ywBusinessQueryRequest);
	}

	@Override
	public void saveOtherAuditInfo(FkAuditConditionEdit fkAuditConditionEdit,
			AbstractSysUser currentUser) {
		OtherAuditInfo otherAuditInfo=new OtherAuditInfo();
		otherAuditInfo.initInsertData(currentUser.getId(), currentUser.getName());
		otherAuditInfo.setBusiId(fkAuditConditionEdit.getBusiId());
		otherAuditInfo.setModeId(ModeIDType.FKTJXGSH.getCode());
		if(fkAuditConditionEdit.getFlag()==true){
			otherAuditInfo.setAuditPostil(AuditEditStatus.SHENHETONGGUO.getValue());
			otherAuditInfo.setAuditStatus(AuditEditStatus.SHENHETONGGUO.getCode().toString());
			fkAuditConditionEdit.setAuditEditStatus(AuditEditStatus.SHENHETONGGUO.getCode());
			//审核通过修改业务extra表中的数据
			updateYwBusinessExtras(fkAuditConditionEdit);
		}else{
			otherAuditInfo.setAuditStatus(AuditEditStatus.SHENHEBUTONGGUO.getCode().toString());
			otherAuditInfo.setAuditPostil(AuditEditStatus.SHENHEBUTONGGUO.getValue());
			fkAuditConditionEdit.setAuditEditStatus(AuditEditStatus.SHENHEBUTONGGUO.getCode());
		}
		otherAuditInfoMapper.insert(otherAuditInfo);
		fkAuditConditionEdit.initUpdateData(currentUser.getId(), currentUser.getName());
		fkAuditConditionEditMapper.updateByPrimaryKeySelective(fkAuditConditionEdit);
		
	}

	private void updateYwBusinessExtras(
			FkAuditConditionEdit fkAuditConditionEdit) {
		FkAuditConditionEditSub fkAuditConditionEditSub1=new FkAuditConditionEditSub();
		fkAuditConditionEditSub1.setBusiId(fkAuditConditionEdit.getBusiId());
		fkAuditConditionEditSub1.setCodeExtras("AddGps");
		fkAuditConditionEditSub1.setDpStatus(DpStatus.NORMAL.getCode());
		fkAuditConditionEditSub1=fkAuditConditionEditSubMapper.selectRefined(fkAuditConditionEditSub1);
		if(fkAuditConditionEditSub1!=null){
			YwBusinessExtras ywBusinessExtras1=new YwBusinessExtras();
			ywBusinessExtras1.setBusiId(fkAuditConditionEdit.getBusiId());
			ywBusinessExtras1.setTypeExtras("Audit");
			ywBusinessExtras1.setCodeExtras("AddGps");
			ywBusinessExtras1.setDpStatus(DpStatus.NORMAL.getCode());
			ywBusinessExtras1=ywBusinessExtrasMapper.selectRefined(ywBusinessExtras1);
			if(ywBusinessExtras1!=null){
				ywBusinessExtras1.setValueExtras(String.valueOf(fkAuditConditionEditSub1.getValueExtrasNew()));
				ywBusinessExtras1.setNameExtras("加装GPS");
				ywBusinessExtrasMapper.updateByPrimaryKeySelective(ywBusinessExtras1);
			}
		}
		FkAuditConditionEditSub fkAuditConditionEditSub2=new FkAuditConditionEditSub();
		fkAuditConditionEditSub2.setBusiId(fkAuditConditionEdit.getBusiId());
		fkAuditConditionEditSub2.setCodeExtras("AddGeposit");
		fkAuditConditionEditSub2.setDpStatus(DpStatus.NORMAL.getCode());
		fkAuditConditionEditSub2=fkAuditConditionEditSubMapper.selectRefined(fkAuditConditionEditSub2);
		if(fkAuditConditionEditSub2!=null){
			YwBusinessExtras ywBusinessExtras2=new YwBusinessExtras();
			ywBusinessExtras2.setBusiId(fkAuditConditionEdit.getBusiId());
			ywBusinessExtras2.setTypeExtras("Audit");
			ywBusinessExtras2.setCodeExtras("AddGeposit");
			ywBusinessExtras2.setDpStatus(DpStatus.NORMAL.getCode());
			ywBusinessExtras2=ywBusinessExtrasMapper.selectRefined(ywBusinessExtras2);
			if(ywBusinessExtras2!=null){
				ywBusinessExtras2.setValueExtras(String.valueOf(fkAuditConditionEditSub2.getValueExtrasNew()));
				ywBusinessExtras2.setNameExtras("加收押金");
				ywBusinessExtrasMapper.updateByPrimaryKeySelective(ywBusinessExtras2);
				
				YwFeesSub ywFeesSub=new YwFeesSub();
				ywFeesSub.setBusiId(fkAuditConditionEdit.getBusiId());
				ywFeesSub.setDpStatus(DpStatus.NORMAL.getCode());
				ywFeesSub.setItemCode("35");
				ywFeesSub=ywFeesSubMapper.selectRefined(ywFeesSub);
				if(ywFeesSub!=null){
					ywFeesSub.setAmountItem(Double.parseDouble(fkAuditConditionEditSub2.getValueExtrasNew()));
//					ywFeesSub.initUpdateData(fkAuditConditionEdit.getModifier(), fkAuditConditionEdit.getModifierName());
					ywFeesSubMapper.updateByPrimaryKeySelective(ywFeesSub);
				}
			}
		}
		FkAuditConditionEditSub fkAuditConditionEditSub3=new FkAuditConditionEditSub();
		fkAuditConditionEditSub3.setBusiId(fkAuditConditionEdit.getBusiId());
		fkAuditConditionEditSub3.setCodeExtras("IsSpecialOrder");
		fkAuditConditionEditSub3.setDpStatus(DpStatus.NORMAL.getCode());
		fkAuditConditionEditSub3=fkAuditConditionEditSubMapper.selectRefined(fkAuditConditionEditSub3);
		if(fkAuditConditionEditSub3!=null){
			YwBusinessExtras ywBusinessExtras3=new YwBusinessExtras();
			ywBusinessExtras3.setBusiId(fkAuditConditionEdit.getBusiId());
			ywBusinessExtras3.setTypeExtras("Audit");
			ywBusinessExtras3.setCodeExtras("IsSpecialOrder");
			ywBusinessExtras3.setDpStatus(DpStatus.NORMAL.getCode());
			ywBusinessExtras3=ywBusinessExtrasMapper.selectRefined(ywBusinessExtras3);
			if(ywBusinessExtras3!=null){
				ywBusinessExtras3.setValueExtras(String.valueOf(fkAuditConditionEditSub3.getValueExtrasNew()));
				ywBusinessExtras3.setNameExtras("特殊订单");
				ywBusinessExtrasMapper.updateByPrimaryKeySelective(ywBusinessExtras3);
			}
		}
		FkAuditConditionEditSub fkAuditConditionEditSub4=new FkAuditConditionEditSub();
		fkAuditConditionEditSub4.setBusiId(fkAuditConditionEdit.getBusiId());
		fkAuditConditionEditSub4.setCodeExtras("FlexibleCharge");
		fkAuditConditionEditSub4.setDpStatus(DpStatus.NORMAL.getCode());
		fkAuditConditionEditSub4=fkAuditConditionEditSubMapper.selectRefined(fkAuditConditionEditSub4);
		if(fkAuditConditionEditSub4!=null){
			YwBusinessExtras ywBusinessExtras4=new YwBusinessExtras();
			ywBusinessExtras4.setBusiId(fkAuditConditionEdit.getBusiId());
			ywBusinessExtras4.setTypeExtras("Audit");
			ywBusinessExtras4.setCodeExtras("FlexibleCharge");
			ywBusinessExtras4.setDpStatus(DpStatus.NORMAL.getCode());
			ywBusinessExtras4=ywBusinessExtrasMapper.selectRefined(ywBusinessExtras4);
			if(ywBusinessExtras4!=null){
				ywBusinessExtras4.setValueExtras(String.valueOf(fkAuditConditionEditSub4.getValueExtrasNew()));
				ywBusinessExtras4.setNameExtras("收费变通");
				ywBusinessExtrasMapper.updateByPrimaryKeySelective(ywBusinessExtras4);
			}
		}
	}
}
